package com.salim.leetcode.$128;

import java.util.*;

public class LongestConsecutiveSequence {
    //找到最小值开始读长度 等于遍历两遍 复杂度n
    private Set<Integer> hashSet = new HashSet<>();
    public int longestConsecutive(int[] nums) {
        if(nums.length==0){
            return 0;
        }
        for(int i:nums){
            hashSet.add(i);
        }
        int max = 1;
        for (Integer key:hashSet){
            if(hashSet.contains(key-1)){
                continue;
            }else{
                int currentLength = 1;
                int currentNum = key+1;
                while (hashSet.contains(currentNum)){
                    currentLength++;
                    currentNum++;
                }
                max = Math.max(max,currentLength);
            }
        }
        return max;
    }

}
